﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using IsmlTools.Common;
using IsmlTools.Dal.SQL;
using IsmlTools.Service;
using RegMatch = System.Text.RegularExpressions.Match;
using Match = IsmlTools.Dal.SQL.Match;

using log4net;
using log4net.Config;
using log4net.Util;
using System.Drawing;


namespace IsmlTools.Bil
{
    public class SaveMatchResult
    {
        private ILog log = LogManager.GetLogger(typeof(SaveMatchResult));

        private WebHelper _webhelper = null;
        private NameHelper _namehelper = null;
        private Dictionary<string, string> _seasonName = null;

        public void SaveResult(int year)
        {
            switch (year)
            {
                case 2011:
                    //SavePreliminary(WebPageLink.PreliminaryPhaseI2011, "Preliminary Phase 1", CalendarHelper.PreliminaryPhaseIDate2011, 4);            
                    //SavePreliminary(WebPageLink.PreliminaryPhaseIi2011, "Preliminary Phase 2", CalendarHelper.PreliminaryPhaseIiDate2011, 4);
                    //SavePreliminary(WebPageLink.PreliminaryPhaseIii2011, "Preliminary Phase 3", CalendarHelper.PreliminaryPhaseIiiDate2011, 4);
                    //SavePreliminary(WebPageLink.Seeding2011, "Seeding", CalendarHelper.Seeding2011, 4);                 
                    SaveRegularSeason(WebPageLink.Aquamarine2011, "Aquamarine", CalendarHelper.Aquamarine2011, 4);
                    SaveRegularSeason(WebPageLink.Topaz2011, "Topaz", CalendarHelper.Topaz2011, 4);
                    SaveRegularSeason(WebPageLink.Amethyst2011, "Amethyst", CalendarHelper.Amethyst2011, 4);
                    SaveRegularSeason(WebPageLink.Sapphire2011, "Sapphire", CalendarHelper.Sapphire2011, 4);
                    SaveRegularSeason(WebPageLink.Emerald2011, "Emerald", CalendarHelper.Emerald2012, 4);
                    SaveRegularSeason(WebPageLink.Ruby2011, "Ruby", CalendarHelper.Ruby2011, 4);
                    SaveRegularSeason(WebPageLink.Diamond2011, "Diamond", CalendarHelper.Diamond2011, 4);
                    //SaveRegularSeason(WebPageLink.Elimination2011, "Elimination", CalendarHelper.Elimination2011, 4);
                    break;
                case 2012:
                    //SavePreliminary(WebPageLink.PreliminaryPhaseI2012, "Preliminary Phase 1", CalendarHelper.PreliminaryPhaseIDate2012, 5);
                    //SavePreliminary(WebPageLink.Seeding2012, "Seeding", CalendarHelper.Seeding2012, 5);
                    //SavePreliminary(WebPageLink.PreliminaryPhaseIi2012, "Preliminary Phase 2", CalendarHelper.PreliminaryPhaseIiDate2012, 5);
                    //SavePreliminary(WebPageLink.PreliminaryPhaseIii2012, "Preliminary Phase 3", CalendarHelper.PreliminaryPhaseIiiDate2012, 5);
                    //SaveRegularSeason(WebPageLink.Aquamarine2012, "Aquamarine", CalendarHelper.Aquamarine2012, 5);
                    //SaveRegularSeason(WebPageLink.Amethyst2012, "Amethyst", CalendarHelper.Amethyst2012, 5);
                    //SaveRegularSeason(WebPageLink.Ruby2012, "Ruby", CalendarHelper.Ruby2012, 5);
                    //SaveRegularSeason(WebPageLink.Emerald2012, "Emerald", CalendarHelper.Emerald2012, 5);
                    //SaveRegularSeason(WebPageLink.Topaz2012, "Topaz", CalendarHelper.Topaz2012, 5);
                    //SaveRegularSeason(WebPageLink.Elimination2012, "Elimination", CalendarHelper.Elimination2012, 5);
                    break;
                case 2013:
                    //SavePreliminary(WebPageLink.PreliminaryPhaseI2013, "Preliminary Phase 1", CalendarHelper.PreliminaryPhaseIDate2013, 6);
                    //SavePreliminary(WebPageLink.PreliminaryPhaseIi2013, "Preliminary Phase 2", CalendarHelper.PreliminaryPhaseIiDate2013, 6);
                    //SavePreliminary(WebPageLink.PreliminaryPhaseIii2013, "Preliminary Phase 3", CalendarHelper.PreliminaryPhaseIiiDate2013, 6);
                    //SaveRegularSeason(WebPageLink.Aquamarine2013, "Aquamarine", CalendarHelper.Aquamarine2013, 6);
                    break;
                default:
                    break;
            }
        }

        private void SaveRegularSeason(string url, string periodName, DateTime dateTime, int seasonID)
        {
            using (ISMLEntities db = new ISMLEntities())
            {
                try
                {
                    _namehelper = new NameHelper();

                    string[] rounds = GetAllRounds(url, periodName);

                    Period period = new Period { Name = periodName, SeasonID = seasonID, PeriodTypeID = null };

                    _seasonName = _namehelper.GetSeasonName(db, (int)period.SeasonID);

                    for (int i = 0; i < rounds.Length; i++)
                    {
                        string gender, age, title, roundName;

                        if (GetPeriodType(periodName) == 2 && i == 7)
                            roundName = "Necklace";
                        else
                            roundName = periodName;

                        string[] matches = GetAllMatches(out title, rounds[i], out gender, out age, roundName, seasonID);

                        if (title == string.Empty)
                            title = "Round " + (i + 1);

                        Round round = new Round { Name = title, Date = dateTime };

                        dateTime = CalendarHelper.GetNextDate(dateTime);
                        if (i == 7)
                            dateTime = CalendarHelper.GetNextDate(dateTime);

                        foreach (string matchstring in matches)
                        {
                            if (matchstring != "\r\n   ")
                            {
                                List<Match> matchinfos = null;
                                if (roundName == "Necklace")
                                    matchinfos = NecklaceGetMatchInfo(matchstring);
                                else
                                    matchinfos = RegularSeasonGetMatchInfo(matchstring, seasonID);

                                foreach (Match match in matchinfos)
                                {
                                    round.Matches.Add(match);
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(gender) && !string.IsNullOrEmpty(age))
                        {
                            round.Matches.Add(GetGenderValue(gender));
                            round.Matches.Add(GetAgeValue(age));
                        }

                        period.Rounds.Add(round);
                        db.Periods.Add(period);
                    }

                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("SavePreliminary2012() failed. Details: " + ex.GetBaseException().Message);
                }
            }

        }

        private string[] GetAllRounds(string pageLink, string periodName)
        {
            _webhelper = new WebHelper();

            string page = _webhelper.ReadPage(pageLink);
            page = page.Replace("Preseason procedures.<br/>\r\n </div>\r\n", "");
            page = page.Replace("<td width=''></td>", "");

            Regex reground = null;

            switch (GetPeriodType(periodName))
            {
                case 1:
                    reground = new Regex(RegHelper.GetMidPattern("<div class='bg block periodB ball10 c mt10'>", " </div>\r\n"));
                    break;
                case 2:
                    reground = new Regex(RegHelper.GetMidPattern("\n <div id=\"res_" + periodName.ToLower() +
                             "-[1]{0,1}[0-9]\" class=\"characterTable\" style=\"display: none;\">[\r]{0,1}\n", " </div>[\r]{0,1}\n"));
                    break;
                default:
                    log.ErrorFormat("GetAllRounds() failed. periodName: " + periodName + " Details: invalid period name");
                    return null;
            }
            return reground.Matches(page)
                   .OfType<System.Text.RegularExpressions.Match>()
                   .Select(m => m.Groups[0].Value)
                   .ToArray();
        }

        private void SavePreliminary(string url, string periodName, DateTime dateTime, int seasonID)
        {
            using (ISMLEntities db = new ISMLEntities())
            {
                try
                {
                    _namehelper = new NameHelper();

                    string[] days = GetAllRounds(url, periodName);

                    Period period = new Period { Name = periodName, SeasonID = seasonID, PeriodTypeID = null };

                    _seasonName = _namehelper.GetSeasonName(db, (int)period.SeasonID);

                    foreach (string day in days)
                    {
                        string gender, age, title;
                        string[] matches = GetAllMatches(out title, day, out gender, out age, periodName, seasonID);

                        Round round = new Round { Name = title, Date = dateTime };
                        dateTime = CalendarHelper.GetNextDate(dateTime);

                        foreach (string matchstring in matches)
                        {
                            if (matchstring != "\r\n   ")
                            {
                                List<Match> matchinfos = GetMatchInfo(matchstring);

                                foreach (Match match in matchinfos)
                                {
                                    round.Matches.Add(match);
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(gender) && !string.IsNullOrEmpty(age))
                        {
                            round.Matches.Add(GetGenderValue(gender));
                            round.Matches.Add(GetAgeValue(age));
                        }

                        period.Rounds.Add(round);
                        db.Periods.Add(period);
                    }

                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("SavePreliminary() failed. Details: " + ex.GetBaseException().Message);
                }
            }
        }

        public List<Match> RegularSeasonGetMatchInfo(string source, int seasonID)
        {
            List<Match> matchlist = new List<Match>();

            string[] matches = Regex.Split(source, "<hr class='short period' size='1' style='margin: 5px auto;'></hr>");

            foreach (string matchstring in matches)
            {
                Match match = new Match();

                string[] lines = Regex.Split(matchstring, @"</tr></table>[\r]{0,1}\n");
                RegMatch regmatch = null;

                regmatch = Regex.Match(lines[0], RegHelper.GetMidPattern("<div class='arenaLabel period2 c'>", "</div>"));
                match.Name = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;

                int num = -1;
                for (int i = 0; i < lines.Length - 1; i++)
                {
                    Vote vote = new Vote();
                    RegMatch m = Regex.Match(lines[i], RegHelper.GetMidPattern("<td class='l'><span id='c", "'>"));
                    vote.SeasonNameCode = m.Success ? m.Groups[0].Value : string.Empty;
                    if (_seasonName.ContainsKey(vote.SeasonNameCode.ToString()))
                        vote.Name = _seasonName[vote.SeasonNameCode.ToString()];
                    m = Regex.Match(lines[i], RegHelper.GetMidPattern("<td class='nLabel r'>", "</td>"));
                    if (m.Success)
                    {
                        int n;
                        if (!int.TryParse(m.Groups[0].Value, out n))
                            n = num;
                        else
                            num = n;

                        vote.Value = n;
                    }
                    else
                        vote.Value = -1;
                    match.Votes.Add(vote);
                }

                regmatch = Regex.Match(lines[lines.Length - 2], RegHelper.GetMidPattern("href='", "'"));

                match.Map = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                if (match.Map == string.Empty)
                {
                    regmatch = Regex.Match(lines[lines.Length - 1], RegHelper.GetMidPattern("href='", "'"));
                    match.Map = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                }

                regmatch = Regex.Match(lines[lines.Length - 2], RegHelper.GetMidPattern("<br/><a href='", "'"));
                match.Chart = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;

                if (match.Chart == string.Empty)
                {
                    regmatch = Regex.Match(lines[lines.Length - 1], RegHelper.GetMidPattern("<br/><a href='", "'"));
                    match.Chart = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                }


                if (!string.IsNullOrEmpty(match.Map) && match.Votes.Count == 2)
                {
                    List<MapRank> mapranklist = GetMapRank(match.Map, seasonID);
                    if (mapranklist != null)
                    {
                        int i = 0;
                        foreach (var vote in match.Votes)
                        {
                            vote.MapRanks.Add(mapranklist[i]);
                            i++;
                        }
                    }
                }
                matchlist.Add(match);
            }

            return matchlist;
        }

        public string[] GetAllMatches(out string title, string round, out string gender, out string age, string roundName, int seasonID)
        {
            gender = string.Empty;
            age = string.Empty;
            title = string.Empty;

            RegMatch match = Regex.Match(round, RegHelper.GetMidPattern("<h2 class='c'>", "</h2>"));
            title = match.Success ? match.Groups[0].Value : string.Empty;

            Regex reggenderage = new Regex(RegHelper.GetMidPattern("<table class='mauto'>", "</td></tr></table>[\r]{0,1}\n"));
            Regex regmatch = null;

            switch (GetRoundType(roundName, seasonID))
            {
                case 1:
                    regmatch = new Regex(RegHelper.GetMidPattern("<div class='ml10 mr10 cursord'>", "\r\n</td></tr></table>\r\n "));
                    break;
                case 2:
                    regmatch = new Regex(RegHelper.GetMidPattern(@"class='gCell' valign='top'>", "</td></tr></table>\r\n</td></tr></table>"));
                    break;
                case 3:
                    regmatch = new Regex(RegHelper.GetMidPattern("<td width='40' class='pr5'>", "</a><br/>[\r]{0,1}\n</td></tr></table>[\r]{0,1}\n"));
                    break;
                case 4:
                    regmatch = new Regex(RegHelper.GetMidPattern("<div class='ml10 mr10 cursord'>", "\n</td></tr></table>\n "));
                    break;
                default:
                    log.ErrorFormat("GetAllMatches() failed. roundName: " + roundName + " Details: invalid period name");
                    return null;
            }

            MatchCollection matchcollection = reggenderage.Matches(round);
            if (matchcollection.Count == 2)
            {
                gender = matchcollection[0].Value;
                age = matchcollection[1].Value;
            }

            return regmatch.Matches(round)
                   .OfType<System.Text.RegularExpressions.Match>()
                   .Select(m => m.Groups[0].Value)
                   .ToArray();
        }

        public List<Match> GetMatchInfo(string source)
        {
            List<Match> matchlist = new List<Match>();

            //string[] matches = Regex.Split(source, "<hr class='short period' size='1' style='margin: 5px auto;'></hr>");
            string[] matches = Regex.Split(source, "<hr class='short period' size='1' style='margin: 5px auto;'");

            // 多行分块的比赛
            string name = string.Empty;

            foreach (string matchstring in matches)
            {
                Match match = new Match();

                string[] lines = Regex.Split(matchstring, @"</tr></table>\r\n");
                RegMatch regmatch = null;

                if (string.IsNullOrEmpty(name))
                {
                    regmatch = Regex.Match(lines[0], RegHelper.GetMidPattern("<h2 class='periodB'>", "</h2>"));
                    match.Name = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                    name = match.Name;
                }
                else
                    match.Name = name;
                //for (int i = 0; i < lines.Length - 2; i++)
                for (int i = 0; i < lines.Length - 1; i++)
                {
                    Vote vote = new Vote();
                    RegMatch m = Regex.Match(lines[i], RegHelper.GetMidPattern("<td class='l'><span id='c", "'>"));
                    vote.SeasonNameCode = m.Success ? m.Groups[0].Value : string.Empty;
                    if (_seasonName.ContainsKey(vote.SeasonNameCode.ToString()))
                    {
                        vote.Name = _seasonName[vote.SeasonNameCode.ToString()];
                        m = Regex.Match(lines[i], RegHelper.GetMidPattern("<td class='nLabel r'>", "</td>"));
                        vote.Value = m.Success ? int.Parse(m.Groups[0].Value) : -1;
                        match.Votes.Add(vote);
                    }
                }

                regmatch = Regex.Match(lines[lines.Length - 2], RegHelper.GetMidPattern("href='", "'"));

                match.Map = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                if (match.Map == string.Empty)
                {
                    regmatch = Regex.Match(lines[lines.Length - 1], RegHelper.GetMidPattern("href='", "'"));
                    match.Map = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                }

                regmatch = Regex.Match(lines[lines.Length - 2], RegHelper.GetMidPattern("<br/><a href='", "'"));
                match.Chart = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;

                if (match.Chart == string.Empty)
                {
                    regmatch = Regex.Match(lines[lines.Length - 1], RegHelper.GetMidPattern("<br/><a href='", "'"));
                    match.Chart = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                }

                if (match.Map.Contains("Chart"))
                {
                    string swap = match.Chart;
                    match.Chart = match.Map;
                    match.Map = swap;
                }

                matchlist.Add(match);
            }
            return matchlist;
        }

        public List<Match> NecklaceGetMatchInfo(string source)
        {
            List<Match> matchlist = new List<Match>();

            string[] matches = Regex.Split(source, "<hr class='short period' size='1' style='margin: 5px auto;'></hr>");

            // 多行分块的比赛
            string name = string.Empty;

            foreach (string matchstring in matches)
            {
                Match match = new Match();

                string[] lines = Regex.Split(matchstring, @"</tr></table>\n");
                RegMatch regmatch = null;

                if (string.IsNullOrEmpty(name))
                {
                    regmatch = Regex.Match(lines[0], RegHelper.GetMidPattern("<h2 class='periodB'>", "</h2>"));
                    match.Name = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                    name = match.Name;
                }
                else
                    match.Name = name;
                if (match.Name != "Necklace Score Calculation")
                {
                    int num = -1;
                    for (int i = 0; i < lines.Length - 2; i++)
                    {
                        Vote vote = new Vote();
                        RegMatch m = Regex.Match(lines[i], RegHelper.GetMidPattern("<td class='l'><span id='c", "'>"));
                        vote.SeasonNameCode = m.Success ? m.Groups[0].Value : string.Empty;
                        if (_seasonName.ContainsKey(vote.SeasonNameCode.ToString()))
                            vote.Name = _seasonName[vote.SeasonNameCode.ToString()];
                        if (match.Name != "Necklace Match Votes")
                            m = Regex.Match(lines[i], RegHelper.GetMidPattern("<td class='nLabel r'>", "</td>"));
                        else
                            m = Regex.Match(lines[i], RegHelper.GetMidPattern("<td class='nLabel r pl10'>", "</td>"));
                        if (m.Success)
                        {
                            int n;
                            if (!int.TryParse(m.Groups[0].Value, out n))
                                n = num;
                            else
                                num = n;

                            vote.Value = n;
                        }
                        else
                            vote.Value = -1;
                        match.Votes.Add(vote);
                    }

                    regmatch = Regex.Match(lines[lines.Length - 2], RegHelper.GetMidPattern("href='", "'"));

                    match.Map = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                    if (match.Map == string.Empty)
                    {
                        regmatch = Regex.Match(lines[lines.Length - 1], RegHelper.GetMidPattern("href='", "'"));
                        match.Map = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                    }

                    regmatch = Regex.Match(lines[lines.Length - 2], RegHelper.GetMidPattern("<br/><a href='", "'"));
                    match.Chart = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;

                    if (match.Chart == string.Empty)
                    {
                        regmatch = Regex.Match(lines[lines.Length - 1], RegHelper.GetMidPattern("<br/><a href='", "'"));
                        match.Chart = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
                    }

                    matchlist.Add(match);
                }
            }
            return matchlist;
        }

        public Match GetGenderValue(string source)
        {
            Match match = new Match();
            match.Name = "Gender";
            Vote male = new Vote();
            male.Name = "Male";
            RegMatch regmatch = Regex.Match(source, RegHelper.GetMidPattern("class='blueFill'>", "</td>"));
            string malevalue = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
            male.Value = int.Parse(malevalue);
            match.Votes.Add(male);
            Vote female = new Vote();
            female.Name = "Famele";
            regmatch = Regex.Match(source, RegHelper.GetMidPattern("class='pinkFill'>", "</td>"));
            string femalevalue = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
            female.Value = int.Parse(femalevalue);
            match.Votes.Add(female);

            return match;
        }

        public Match GetAgeValue(string source)
        {
            Match match = new Match();
            match.Name = "Age";
            Vote adult = new Vote();
            adult.Name = "Adult";
            RegMatch regmatch = Regex.Match(source, RegHelper.GetMidPattern("class='orngFill'>", "</td>"));
            string adultvalue = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
            adult.Value = int.Parse(adultvalue);
            match.Votes.Add(adult);
            Vote minor = new Vote();
            minor.Name = "Minor";
            regmatch = Regex.Match(source, RegHelper.GetMidPattern("class='grenFill'>", "</td>"));
            string minorvalue = regmatch.Success ? regmatch.Groups[0].Value : string.Empty;
            minor.Value = int.Parse(minorvalue);
            match.Votes.Add(minor);
            return match;
        }

        public int GetPeriodType(string periodName)
        {
            if (periodName == "Preliminary Phase 1" || periodName == "Seeding" || periodName == "Preliminary Phase 2" ||
                periodName == "Preliminary Phase 3")
                return 1;
            else
                return 2;
        }

        public int GetRoundType(string roundName, int seasonID)
        {
            if (roundName == "Preliminary Phase 1" || roundName == "Preliminary Phase 2" ||
                roundName == "Preliminary Phase 3")
                return 1;
            else if (roundName == "Seeding")
            {
                if (seasonID == 4) return 1;
                else return 2;
            }
            else if (roundName == "Necklace")
                return 4;
            else
                return 3;
        }

        public List<MapRank> GetMapRank(string mapUrl, int seasonID)
        {
            MapRank maprank1 = new MapRank();
            MapRank maprank2 = new MapRank();
            List<NCode> Node = MapHelper.Node2012;
            Dictionary<int, int> ColorMappling = MapHelper.ColorMappling2012;

            if (seasonID == 4)
            {
                Node = MapHelper.Node2011;
                ColorMappling = MapHelper.ColorMappling2011;
            }

            try
            {
                string pathname = _webhelper.DownloadFile(mapUrl);
                Bitmap map = new Bitmap(pathname);

                foreach (var node in Node)
                {
                    Color color = map.GetPixel(node.X, node.Y);
                    int rank = 99;

                    int tmp = color.ToArgb();

                    if (seasonID == 4)
                    {
                        rank = GetColorRank(tmp);
                        if (rank == 88)
                            return null;
                    }
                    else
                    {
                        if (ColorMappling.ContainsKey(tmp))
                            rank = ColorMappling[color.ToArgb()];
                        else
                            return null;
                    }
                    switch (node.Code)
                    {
                        case "Canada":
                            maprank1.Canada = rank;
                            maprank2.Canada = -maprank1.Canada;
                            break;
                        case "China":
                            maprank1.China = rank;
                            maprank2.China = -maprank1.China;
                            break;
                        case "Russia":
                            maprank1.Russia = rank;
                            maprank2.Russia = -maprank1.Russia;
                            break;
                        case "USA":
                            maprank1.USA = rank;
                            maprank2.USA = -maprank1.USA;
                            break;
                        case "Africa":
                            maprank1.Africa = rank;
                            maprank2.Africa = -maprank1.Africa;
                            break;
                        case "SouthAmerica":
                            maprank1.SouthAmerica = rank;
                            maprank2.SouthAmerica = -maprank1.SouthAmerica;
                            break;
                        case "EastEurope":
                            maprank1.EastEurope = rank;
                            maprank2.EastEurope = -maprank1.EastEurope;
                            break;
                        case "Japan":
                            maprank1.Japan = rank;
                            maprank2.Japan = -maprank1.Japan;
                            break;
                        case "WesternEurope":
                            maprank1.WesternEurope = rank;
                            maprank2.WesternEurope = -maprank1.WesternEurope;
                            break;
                        case "Australia":
                            maprank1.Australia = rank;
                            maprank2.Australia = -maprank1.Australia;
                            break;
                        case "Korea":
                            maprank1.Korea = rank;
                            maprank2.Korea = -maprank1.Korea;
                            break;
                        case "Taiwan":
                            maprank1.Taiwan = rank;
                            maprank2.Taiwan = -maprank1.Taiwan;
                            break;
                        case "CentralEurope":
                            maprank1.CentralEurope = rank;
                            maprank2.CentralEurope = -maprank1.CentralEurope;
                            break;
                        case "Vietnam":
                            maprank1.Vietnam = rank;
                            maprank2.Vietnam = -maprank1.Vietnam;
                            break;
                        case "Thailand":
                            maprank1.Thailand = rank;
                            maprank2.Thailand = -maprank1.Thailand;
                            break;
                        case "Indonesia":
                            maprank1.Indonesia = rank;
                            maprank2.Indonesia = -maprank1.Indonesia;
                            break;
                        case "Philippines":
                            maprank1.Philippines = rank;
                            maprank2.Philippines = -maprank1.Philippines;
                            break;
                        case "HongKong":
                            maprank1.HongKong = rank;
                            maprank2.HongKong = -maprank1.HongKong;
                            break;
                        case "Malaysia":
                            maprank1.Malaysia = rank;
                            maprank2.Malaysia = -maprank1.Malaysia;
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
            List<MapRank> mapranklist = new List<MapRank>();
            mapranklist.Add(maprank1);
            mapranklist.Add(maprank2);
            return mapranklist;
        }

        public int GetColorRank(int color)
        {
            if (color > -1613310 && color < -1213310)
                return -1;
            else if (color > -12603222 && color < -12203222)
                return 1;
            else if (color > -7108266 && color < -6708266)
                return 0;
            else
                return 88;
        }
    }
}
